package com.zt.yongxinbao.config;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
    public class CORSFilter implements Filter {
//        @Bean
//        public CorsFilter corsFilter() {
//            CorsConfiguration config = new CorsConfiguration();
//            //允许所有域名进行跨域调用
//            config.addAllowedOrigin("*");
//            //允许跨越发送cookie
//            config.setAllowCredentials(true);
//            //放行全部原始头信息
//            config.addAllowedHeader("*");
//            //允许所有请求方法跨域调用
//            config.addAllowedMethod("*");
//            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//            source.registerCorsConfiguration("/**", config);
//            return new CorsFilter(source);
//        }
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;

            response.setHeader("Access-Control-Allow-Origin", "*");

            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");

            response.setHeader("Access-Control-Max-Age", "3600");

            response.setHeader("Access-Control-Allow-Headers", "*");
            chain.doFilter(req, res);
        }
        @Override
        public void destroy() {
        }
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    }



